import chalk from "chalk";
import dayjs from "dayjs";

enum LogLevel {
  Info = "Info",
  Warn = "Warn",
  Error = "Error",
  Fatal = "Fatal",
}

/***
 * 日志模块
 */
export class Logger {
  constructor(private prefix: string) {}

  private log(text: string) {
    console.log(text);
    //TODO 这里可以把日志存进日志数据库（如HBase + ES）后续做数据分析
  }

  info(...args: any[]) {
    const text = chalk.blue(`${dayjs().format("YYYY-MM-DD HH:mm:ss")}[${this.prefix}][${LogLevel.Info}]:`, ...args);
    this.log(text);
  }

  warn(...args: any[]) {
    const text = chalk.yellow(`${dayjs().format("YYYY-MM-DD HH:mm:ss")}[${this.prefix}][${LogLevel.Warn}]:`, ...args);
    this.log(text);
  }

  error(...args: any[]) {
    const text = chalk.red(`${dayjs().format("YYYY-MM-DD HH:mm:ss")}[${this.prefix}][${LogLevel.Error}]:`, ...args);
    this.log(text);
  }

  fatal(...args: any[]) {
    const text = chalk.red(`${dayjs().format("YYYY-MM-DD HH:mm:ss")}[${this.prefix}][${LogLevel.Fatal}]:`, ...args);
    this.log(text);
  }
}
